class CentralView extends eui.Component implements  eui.UIComponent {
	public item1:NumItem;
	public item2:NumItem;
	public item3:NumItem;
	public item4:NumItem;
	public item5:NumItem;
	public item6:NumItem;
	public item7:NumItem;
	public item8:NumItem;
	public item9:NumItem;
	public item10:NumItem;
	public item11:NumItem;
	public item12:NumItem;
	public item13:NumItem;
	public item14:NumItem;
	public item15:NumItem;
	public item16:NumItem;

	private COLOR:Array<number>;
	public static MOVETIME = 50;

	private itemArray:Array<NumItem[]>;

	public constructor() {
		super();

		this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
		this.skinName = "resource/game/CentralView.exml";
	}

	private onComplete(event:Event)
	{
		this.itemArray = new Array<NumItem[]>();
		this.itemArray[0] = new Array<NumItem>();
		this.itemArray[1] = new Array<NumItem>();
		this.itemArray[2] = new Array<NumItem>();
		this.itemArray[3] = new Array<NumItem>();
		this.itemArray[0][0] = this.item1;
		this.itemArray[0][1] = this.item2;
		this.itemArray[0][2] = this.item3;
		this.itemArray[0][3] = this.item4;
		this.itemArray[1][0] = this.item5;
		this.itemArray[1][1] = this.item6;
		this.itemArray[1][2] = this.item7;
		this.itemArray[1][3] = this.item8;
		this.itemArray[2][0] = this.item9;
		this.itemArray[2][1] = this.item10;
		this.itemArray[2][2] = this.item11;
		this.itemArray[2][3] = this.item12;
		this.itemArray[3][0] = this.item13;
		this.itemArray[3][1] = this.item14;
		this.itemArray[3][2] = this.item15;
		this.itemArray[3][3] = this.item16;

		for (let i = 0; i < 4; i++)
		{
			for (let j = 0; j < 4; j++)
			{
				this.itemArray[i][j].visible = false;
			}
		}

		this.COLOR = new Array<number>();
		this.COLOR[2] = 0x33ffff;
		this.COLOR[4] = 0x33ccff;
		this.COLOR[8] = 0x3399ff;
		this.COLOR[16] = 0x3366ff;
		this.COLOR[32] = 0x3333ff;
		this.COLOR[64] = 0x3300ff;
		this.COLOR[128] = 0x330099;
		this.COLOR[256] = 0x330066;
		this.COLOR[512] = 0x330033;
		this.COLOR[1024] = 0x660033;
		this.COLOR[2048] = 0x660000;
		this.COLOR[4096] = 0x330033;
		this.COLOR[8192] = 0x330000;
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}

	protected childrenCreated():void
	{
		super.childrenCreated();
	}

	public TweenScale(target: any, callback: Function = null, thisObject?: any, params?: Array<any>): void {
        if (target.tween_count) {
            return;
        }
        target.touchEnabled = false;
        var lastScaleX = target.scaleX;
        var lastScaleY = target.scaleY;
        target.scaleX = 0.7*lastScaleX;
        target.scaleY = 0.7*lastScaleY;
        if (callback) {
            egret.Tween.get(target).to(
                { scaleX: lastScaleX, scaleY: lastScaleY },
                300,
                egret.Ease.backOut).call(()=>{
                    target.touchEnabled = true;
                    callback.apply(thisObject);
                }, thisObject, params);
        } else {
            egret.Tween.get(target).to(
                { scaleX: lastScaleX, scaleY: lastScaleY },
                300,
                egret.Ease.backOut).call(()=>{
                    target.touchEnabled = true;
                });
        }
    }

	private combTable:Array<number[]>;
	private lastPosition:number;
	public scaleNum()
	{
		for (let i = 0; i < 4; i++)
		{
			for (let j = 0; j < 4; j++)
			{
				if (this.combTable[i][j] == 1)
				{
					switch(this.lastPosition)
					{
					case MainLogic.UP:
						this.TweenScale(this.itemArray[j][i]);
						break;
					case MainLogic.DOWN:
						this.TweenScale(this.itemArray[3-j][i]);
						break;
					case MainLogic.LEFT:
						this.TweenScale(this.itemArray[i][j]);
						break;
					case MainLogic.RIGHT:
						this.TweenScale(this.itemArray[i][3-j]);
						break;
					default:
						break;
					}
				}
			}
		}
	}

	public showData(gameData:Array<number[]>)
	{
		for (let i = 0; i < 4; i++)
		{
			for (let j = 0; j < 4; j++)
			{
				let num = gameData[i][j];
				this.itemArray[i][j].num.text = "" + num;
				this.itemArray[i][j].bgRect.fillColor = this.COLOR[num] ? this.COLOR[num] : this.COLOR[8192];
				if (num > 0)
					this.itemArray[i][j].visible = true;
				else
					this.itemArray[i][j].visible = false;
				if (num > 999)
					this.itemArray[i][j].num.size = 55;
				else
					this.itemArray[i][j].num.size = 65;
			}
		}
	}

	// 移动到目标下标（i, j）
	public move_num(num:NumItem, i:number, j:number)
	{
		egret.Tween.get(num).to({x:this.getX(i, j), y:this.getY(i, j)}, CentralView.MOVETIME);
	}

	public move(move_table:Array<number[]>, position:number, comb_table:Array<number[]>)
	{
		for (let i = 0; i < 4; i++)
		{
			for (let j = 0; j < 4; j++)
			{
				if (move_table[i][j] != j)
				{
					switch(position)
					{
					case MainLogic.UP:
						this.move_num(this.itemArray[j][i], move_table[i][j], i);
						break;
					case MainLogic.DOWN:
						this.move_num(this.itemArray[3-j][i], 3 - move_table[i][j], i);
						break;
					case MainLogic.LEFT:
						this.move_num(this.itemArray[i][j], i, move_table[i][j]);
						break;
					case MainLogic.RIGHT:
						this.move_num(this.itemArray[i][3-j], i, 3 - move_table[i][j]);
						break;
					default:
						break;
					}
				}
			}
		}
		this.combTable = comb_table;
		this.lastPosition = position;
	}

	// 获取方格[i][j]位置的坐标
	public getX(i:number, j:number):number
	{
		const offsetX = 20;
		const anchorOffset = 82.5;
		return offsetX + j * 165 + anchorOffset;
	}
	public getY(i:number, j:number):number
	{
		const offsetY = 20;
		const anchorOffset = 82.5;
		return offsetY + i * 165 + anchorOffset;
	}

	public resetPosition()
	{
		for (let i = 0; i < 4; i++)
		{
			for (let j = 0; j < 4; j++)
			{
				this.itemArray[i][j].x = this.getX(i, j);
				this.itemArray[i][j].y = this.getY(i, j);
			}
		}
	}
	
}